<!-- 档案借阅审批 -->
<template>
  <div class="app-container">
    <!--头部搜索-->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="档案名称" prop="typeName">
        <el-select v-model="queryParams.typeName" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in typeOptions"
            :key="item.typeId"
            :label="item.typeName"
            :value="item.typeName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题名" prop="fileTitle">
        <el-input
          v-model="queryParams.fileTitle"
          placeholder="请输入题名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子借阅" prop="online">
        <el-select v-model="queryParams.online" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实体借阅" prop="offline">
        <el-select v-model="queryParams.offline" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditState">
        <el-select v-model="queryParams.auditState" filterable clearable placeholder="请选择审核状态">
          <el-option
            v-for="item in optionAudit"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeApplyTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker
          v-model="daterangeAuditTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:borrow:remove']"
          v-if="isAdmin"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:borrow:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleApprove"
          v-hasPermi="['system:borrow:approve']"
        >审批通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleReject"
          v-hasPermi="['system:borrow:reject']"
        >审批拒绝</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleGiveBack"
          v-hasPermi="['system:borrow:giveback']"
        >实体借阅归还</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="borrowList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="档号" align="center" prop="docNum" />
      <el-table-column label="全宗号" align="center" prop="fullAnces" />
      <el-table-column
        label="题名"
        align="center"
        prop="fileTitle"
        min-width="300px"
        show-overflow-tooltip
      />
      <el-table-column label="档案名称" align="center" prop="typeName" />
      <el-table-column label="文件类型" align="center" prop="tbType">
        <template slot-scope="scope">
          <span v-if="scope.row.tbType === '1'">案卷文件</span>
          <span v-if="scope.row.tbType === '2'">卷内文件</span>
          <span v-if="scope.row.tbType === '3'">独立文件</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="userName" />
      <el-table-column label="单位" align="center" prop="workUnit" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="岗位" align="center" prop="postName" />
      <el-table-column
        label="借阅说明"
        align="center"
        prop="borDetail"
        min-width="300px"
        show-overflow-tooltip
      />
      <el-table-column
        label="利用目的"
        align="center"
        prop="purpose"
        min-width="300px"
        show-overflow-tooltip
      />
      <el-table-column label="电子借阅" align="center" prop="online">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.online == 0">否</el-tag>
          <el-tag type="success" v-if="scope.row.online == 1">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="实体借阅" align="center" prop="offline">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.offline == 0">否</el-tag>
          <el-tag type="success" v-if="scope.row.offline == 1">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="借阅天数" align="center" prop="dayNum" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人员" align="center" prop="auditUser" />
      <el-table-column label="实体借阅归还状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.offline == 0">无需归还</el-tag>
          <el-tag type="info" v-else-if="scope.row.auditState != 2">无需归还</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status == 0">未归还</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 1">已归还</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="归还时间" align="center" prop="backTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.backTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditState" fixed="right">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditState == 1">申请中</el-tag>
          <el-tag type="success" v-if="scope.row.auditState == 2">审核通过</el-tag>
          <el-tag type="danger" v-if="scope.row.auditState == 3">审核拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:borrow:edit']"
            v-if="scope.row.auditState !== '2'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:borrow:remove']"
            v-if="scope.row.auditState !== '2'"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改借阅管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主键ID" prop="borrow_id">
          <el-input v-model="form.archivesId" placeholder="请输入案卷ID" disabled />
        </el-form-item>
        <el-form-item label="题名" prop="fileTitle">
          <el-input v-model="form.fileTitle" placeholder="请输入题名" disabled />
        </el-form-item>
        <el-form-item label="申请人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="单位" prop="workUnit">
          <el-input v-model="form.workUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="部门" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="借阅人岗位" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入借阅人岗位" />
        </el-form-item>
        <el-form-item label="借阅说明" prop="borDetail">
          <el-input v-model="form.borDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="利用目的" prop="purpose">
          <el-input v-model="form.purpose" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="电子借阅" prop="online">
          <el-checkbox v-model="check_online">电子借阅</el-checkbox>
        </el-form-item>
        <el-form-item label="实体借阅" prop="offline">
          <el-checkbox v-model="check_offline">实体借阅</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { treeselect, getType, listType } from "@/api/system/type";

import {
  listBorrow,
  getBorrow,
  delBorrow,
  addBorrow,
  updateBorrow,
  updateApprove,
  updateReject,
  updateGiveBack
} from "@/api/system/borrow";

export default {
  name: "Borrow",
  // 组件传参
  props: {
    // 表对象集合
    subData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      /******************借阅内容************/
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务数据表名称
      tbName: null,
      // 档案分类表格数据
      typeOptions: undefined,
      // 用户id
      userId: null,
      // 借阅管理表格数据
      borrowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 申请时间范围
      daterangeApplyTime: [],
      // 审核时间范围
      daterangeAuditTime: [],
      // 归还时间范围
      daterangeBackTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archivesId: null,
        userId: null,
        userName: null,
        workUnit: null,
        deptName: null,
        postName: null,
        online: null,
        offline: null,
        auditState: null,
        applyTime: null,
        auditTime: null,
        auditUser: null,
        status: null,
        docNum: null,
        typeName: null,
        backTime: null,
        fileTitle: null
      },
      // 审核选项
      optionAudit: [
        {
          label: "申请中",
          value: "1"
        },
        {
          label: "审核通过",
          value: "2"
        },
        {
          label: "审核拒绝",
          value: "3"
        }
      ],
      // 是，否
      option: [
        {
          label: "否",
          value: "0"
        },
        {
          label: "是",
          value: "1"
        }
      ],
      check_online: true,
      check_offline: false,
      // 表单参数
      form: {},
      // 判断是否超级管理员
      isAdmin: false,
      // 表单校验
      rules: {}
    };
  },
  created() {
    // 获取档案类型数据
    this.getTypeOptions();
    // 判断是否超级管理员或管理员
    this.getIsAdmin().then(res => {
      this.isAdmin = res;
    })
    // 获取借阅数据
    this.getList();
  },
  methods: {
    // 获取档案类型数据
    getTypeOptions() {
      listType({ status: "0", tbFlag: '1', isCreateTb: '1' }).then(response => {
        this.typeOptions = response.data;
      });
    },
    /** 查询借阅管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeApplyTime && "" != this.daterangeApplyTime) {
        this.queryParams.params["beginApplyTime"] = this.daterangeApplyTime[0];
        this.queryParams.params["endApplyTime"] = this.daterangeApplyTime[1];
      }
      if (null != this.daterangeAuditTime && "" != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] = this.daterangeAuditTime[0];
        this.queryParams.params["endAuditTime"] = this.daterangeAuditTime[1];
      }
      if (null != this.daterangeBackTime && "" != this.daterangeBackTime) {
        this.queryParams.params["beginBackTime"] = this.daterangeBackTime[0];
        this.queryParams.params["endBackTime"] = this.daterangeBackTime[1];
      }
      this.queryParams.tbName = this.tbName;
      listBorrow(this.queryParams).then(response => {
        this.borrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        borrowId: null,
        archivesId: null,
        userId: null,
        userName: null,
        workUnit: null,
        deptName: null,
        postName: null,
        borDetail: null,
        purpose: null,
        online: null,
        offline: null,
        auditState: null,
        applyTime: null,
        auditTime: null,
        auditUser: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        status: null,
        docNum: null,
        typeName: null,
        backTime: null,
        fileTitle: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeApplyTime = [];
      this.daterangeAuditTime = [];
      this.daterangeBackTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.borrowId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加借阅管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const borrowId = row.borrowId || this.ids;
      getBorrow(borrowId).then(response => {
        this.check_online = response.data.online === "1" ? true : false;
        this.check_offline = response.data.offline === "1" ? true : false;
        this.form = response.data;
        this.open = true;
        this.title = "修改借阅管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.borrowId != null) {
            this.form.online = this.check_online ? 1 : 0;
            this.form.offline = this.check_offline ? 1 : 0;
            updateBorrow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (!this.check_online && !this.check_offline) {
              this.$message.error("请选择借阅类型！");
            } else {
              this.form.online = this.check_online ? 1 : 0;
              this.form.offline = this.check_offline ? 1 : 0;
              addBorrow(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const borrowIds = row.borrowId || this.ids;
      this.$modal
        .confirm('是否确认删除借阅管理编号为"' + borrowIds + '"的数据项？')
        .then(function() {
          return delBorrow(borrowIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/borrow/export",
        {
          ...this.queryParams
        },
        `borrow_${new Date().getTime()}.xlsx`
      );
    },
    // 审批通过按钮
    handleApprove() {
      const borrowIds = this.ids;
      this.$modal
        .confirm('是否确认审批通过借阅编号为"' + borrowIds + '"的数据项？')
        .then(function() {
          return updateApprove(borrowIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审批成功");
        })
        .catch(() => {});
    },
    // 审批拒绝按钮
    handleReject() {
      const borrowIds = this.ids;
      this.$modal
        .confirm('是否确认审批拒绝借阅编号为"' + borrowIds + '"的数据项？')
        .then(function() {
          return updateReject(borrowIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("审批成功");
        })
        .catch(() => {});
    },
    // 实体借阅归还
    handleGiveBack() {
      const borrowIds = this.ids;
      this.$modal
        .confirm('是否确认归还实体借阅编号为"' + borrowIds + '"的数据项？')
        .then(function() {
          return updateGiveBack(borrowIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("归还成功");
        })
        .catch(() => {});
    }
  }
};
</script>
